<?php

/**
 * 任务执行管理
 */
class action
{

    private $per_page = 100;

    /**
     * 任务执行列表
     *
     * @throws AnException
     */
    public function index() {

        $search_filter = $page_no = $status = $job_id = null;

        extract(URLTracker::init(array(
            'search_filter' => array(),
            'page_no' => 1,
            'status' => '-1',
            'job_id' => 0,
        )));

        $filter = array();

        if ('-1' != $status) {
            $filter['status'] = $status;
        } else {
            $filter['status >='] = '0';
        }

        if ($job_id == 0) {
            $job_id = topic_helper::get_default_job_id();
            URLTracker::set('job_id', $job_id, 1);
        }

        $job_list = topic_helper::get_job_list();
        $job_name = $job_list[$job_id]['name'];

        // 合伙人筛选
        if (isset($search_filter['dealer_name']) && $search_filter['dealer_name']) {
            $user_info = user_helper::get_user_info_by_relname(trim($search_filter['dealer_name']));
            if (!empty($user_info)) {
                $filter['parent_id'] = $user_info['id'];
            }
        }
        if (isset($search_filter['dealer_mobile']) && $search_filter['dealer_mobile']) {
            $user_info = user_helper::get_user_info_by_username(trim($search_filter['dealer_mobile']));
            if (!empty($user_info)) {
                $filter['parent_id'] = $user_info['id'];
            }
        }

        // 时间筛选
        if (isset($search_filter['start_date']) && $search_filter['start_date']) {
            $filter['upload_time >='] = $search_filter['start_date'] . ' 00:00:00';
        }
        if (isset($search_filter['end_date']) && $search_filter['end_date']) {
            $filter['upload_time <='] = $search_filter['end_date'] . ' 23:59:59';
        }

        $filter['job_id'] = $job_id;

        $job_comply_count = _model('topic_job_comply')->getTotal(enable_filter($filter));
        $job_comply_list = array();
        if ($job_comply_count) {
            $pager = new Pager($this->per_page);
            if ($pager->generate($job_count)) {
                Response::assign('pager', $pager);
            }
            $job_comply_list = get_data_list('topic_job_comply', enable_filter($filter), ' ORDER BY `upload_time` DESC', $pager->getLimit($page_no));
            foreach ($job_comply_list as $key => $item) {
                $store_info = store_helper::get_store_info($item['store_id']);
                $store_info['cover'] = store_helper::cover($store_info['cover']);

                $job_comply_list[$key]['store_info'] = $store_info;

                $job_comply_list[$key]['dealer_info'] = user_helper::get_user_info($item['parent_id']);
                if ($item['parent_id'] == $item['uploader_id']) {
                    $job_comply_list[$key]['uploader_info'] = $job_comply_list[$key]['dealer_info'];
                } else {
                    $job_comply_list[$key]['uploader_info'] = user_helper::get_user_info($item['uploader_id']);
                }

                $pics = json_decode($item['pics'], 1);
                foreach ($pics as $p => $pic) {
                    $pics[$p] = _image($pic);
                }
                $job_comply_list[$key]['pics'] = $pics;

                if (!empty($item['extra'])) {
                    $job_comply_list[$key]['extra'] = nl2br($item['extra']);
                }

                if (!empty($item['reject_reason'])) {
                    $job_comply_list[$key]['reject_reason'] = nl2br($item['reject_reason']);
                }
            }
        }

        Response::assign('job_name', $job_name);
        Response::assign('search_filter', $search_filter);
        Response::assign('job_list', $job_list);
        Response::assign('job_comply_list', $job_comply_list);
        Response::display('admin/job_comply_list.html');
    }

    /**
     * AJAX 审批
     *
     * @throws AnException
     */
    public function verify() {
        $action = an_request('action', '');
        $id = an_request('id', 0);

        if (!$action || $id == 0) {
            return_data(1, '无效的操作');
        }

        $data = array();

        switch ($action) {
            case 'accept':
                $selected = intval(an_request('selected', -1));
                if ($selected < 0) {
                    return_data(1, '未选择任何图片');
                }
                $info = _uri('topic_job_comply', $id);
                if (empty($info)) {
                    return_data(1, '数据错误');
                }
                $pics = json_decode($info['pics'], true);
                $pics = array_slice($pics, $selected, 1);
                $data['pics'] = json_encode($pics);
                $data['status'] = 1;
                break;
            case 'reject':
                $reject_reason = trim(an_request('reject_reason', ''));
                if (empty($reject_reason)) {
                    return_data(1, '请填写驳回理由');
                }
                $data['status'] = 2;
                $data['reject_reason'] = $reject_reason;
                break;
        }

        $ret = _model('topic_job_comply')->update(array('id' => $id), $data);

        if ($ret) {
            return_data(0, '审批成功', _uri('topic_job_comply', $id));
        }

        return_data(1, '审批失败, 请稍后重试');
    }

    public function export($filter = array(), $filename = '') {

        $search_filter = $status = $job_id = null;

        extract(URLTracker::init(array(
            'search_filter' => array(),
            'status' => '-1',
            'job_id' => 0,
        )));

        $filename = '';
        $filter = array();

        $filter['job_id'] = $job_id;

        $job_list = topic_helper::get_job_list();
        $job_name = $job_list[$job_id]['name'];

        $filename .= $job_name;

        // 时间筛选
        $filename .= '_';
        if (isset($search_filter['start_date']) && $search_filter['start_date']) {
            $filter['upload_time >='] = $search_filter['start_date'] . ' 00:00:00';
            $filename .= $search_filter['start_date'];
        } else {
            $filename .= '-';
        }
        $filename .= '~';
        if (isset($search_filter['end_date']) && $search_filter['end_date']) {
            $filter['upload_time <='] = $search_filter['end_date'] . ' 23:59:59';
            $filename .= $search_filter['end_date'];
        } else {
            $filename .= '-';
        }

        $job_comply_list = _model('topic_job_comply')->getList(enable_filter($filter), ' ORDER BY `upload_time` DESC');

        $row = array(
            array('序号', '合伙人', '上传者', '门店ID', '门店名称', '门店后缀', '上传时间', '上传备注', '状态', '驳回理由')
        );
        if ($job_comply_list) {
            foreach ($job_comply_list as $k => $v) {
                $store_info = store_helper::get_store($v['store_id']);
                $dealer_info = user_helper::get_user_info($v['parent_id']);
                $uploader_info = user_helper::get_user_info($v['uploader_id']);

                $row[] = array(
                    $v['id'],
                    $dealer_info['realname'],
                    $uploader_info['realname'],
                    $v['store_id'],
                    str_replace(array("\n", "\t", "\r"), '', $store_info['title']),
                    str_replace(array("\n", "\t", "\r"), '', $store_info['title_branch']),
                    $v['upload_time'],
                    $v['extra'],
                    topic_config::$COMPLY_STATUS[intval($v['status'])],
                    ($v['status'] == 2 ? $v['reject_reason'] : '')
                );
            }
        }

        _widget('tools.csv')->export($filename . '_' . time(), $row);
    }

    public function download() {

    }
}